/**
 * anis.darwich
 */

package com;

public class MotionCommand {
	
	private Integer start_delimiter;
	private Integer api_type;

	private Integer x_coord;
	private Integer y_coord;
	
	private Integer end_delimiter;

	public MotionCommand (Integer x, Integer y) {
		this.start_delimiter = 0;
		this.api_type = 5;
		
		this.x_coord = x;
		this.y_coord = y;
		
		this.end_delimiter = 254;
	}
	
	public MotionCommand () {
		this.start_delimiter = 0;
		this.end_delimiter = 254;
	}
	
	public Integer getApiType() {
		return this.api_type;
	}
	
	public Integer getX() {
		return this.x_coord;
	}
	
	public Integer getY() {
		return this.y_coord;
	}
	
	public String toHexPrint(){
		return (Integer.toHexString(start_delimiter) + Integer.toHexString(api_type) + Integer.toHexString(x_coord) + Integer.toHexString(y_coord) + Integer.toHexString(end_delimiter));
	}
	
	public byte[] toSend() {
		byte[] send_buffer = new byte[5];
		send_buffer[0] = this.start_delimiter.byteValue();
		send_buffer[1] = this.api_type.byteValue();
		send_buffer[2] = this.x_coord.byteValue();
		send_buffer[3] = this.y_coord.byteValue();
		send_buffer[4] = this.end_delimiter.byteValue();
		return send_buffer;
	}
	
	public static MotionCommand parse(String packet) {
		MotionCommand received = new MotionCommand();
		received.api_type = Integer.parseInt(packet.substring(2, 4), 16);
		received.x_coord = Integer.parseInt(packet.substring(4, 6), 16);
		received.y_coord = Integer. parseInt(packet.substring(6, 8), 16);
		
		/*
		System.out.print("length " + tmp.length()+ "\n"); // 8
		System.out.print("@ index 0-2: " + tmp.substring(0, 2) + "\n"); // 00 start delimiter
		System.out.print("@ index 2-4: " + tmp.substring(2, 4) + "\n"); // 05 api type
		System.out.print("@ index 4-6: " + tmp.substring(4, 6) + "\n"); // 10 x
		System.out.print("@ index 6-8: " + tmp.substring(6, 8) + "\n"); // 20 y
		*/
		
		return received;
	}
}	
	